package com.zmn.plat.dubbo.impl.part;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import javax.annotation.Resource;

import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.cglib.beans.BeanCopier;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.business.interfaces.part.category.PartCategoryBService;
import com.zmn.plat.common.dto.part.PartCategoryDRO;
import com.zmn.plat.dubbo.interfaces.part.PartSkuCategoryListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.part.PartCategory;
import com.zmn.plat.services.interfaces.part.PartCategoryService;
import com.zmn.plat.utils.bean.BeanCopyUtils;

/**
 * 类描述：
 *
 * @author DaiZhichao
 * @version v1.0
 * @since 2018/10/25 9:47
 */

@Service(loadbalance = "roundrobin", timeout = 5000, retries = -1, version = DubboConsts.INTERFACE_VERSION)
public class PartSkuCategoryListRemoteServiceImpl implements PartSkuCategoryListRemoteService {

    private Logger logger = LoggerFactory.getLogger(PartSkuCategoryListRemoteServiceImpl.class);

    @Resource
    PartCategoryBService partCategoryBService;
    @Resource
    PartCategoryService partCategoryService;
    public static final String TAG = "配件分类 dubbo 接口";

    /**
     * 描述:获取一级配件分类信息详情列表
     *
     * @return ResponseDTO<PartCategoryDRO>
     * @author DaiZhichao
     * @since 2018/10/25 9:55
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<PartCategoryDRO>> listFirstLevelCategory() {
        return listSecondLevelCategoryByParentCategoryId(GlobalConsts.TOP_ID);
    }

    /**
     * 描述:获取一级配件分类信息简单列表
     *
     * @return ResponseDTO<PartCategoryDRO>
     * @author DaiZhichao
     * @since 2018/10/25 9:55
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listFirstLevelSimpleCategory() {
        return listSecondLevelSimpleCategoryByParentCategoryId(GlobalConsts.TOP_ID);
    }

    /**
     * 描述:通过一级配件分类ID/二级配件分类ID获取分类详情
     *
     * @param categoryId
     *            配件分类ID
     * @return ResponseDTO<PartCategoryDRO>
     * @author DaiZhichao
     * @since 2018/10/31 10:58
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<PartCategoryDRO> getCategoryDetailByCategoryId(Integer categoryId) {
        PartCategoryDRO partCategoryDRO = new PartCategoryDRO();
        try {
            PartCategory partCategory = partCategoryService.findByKey(categoryId);
            BeanCopyUtils.copyProperties(partCategory, partCategoryDRO);
        } catch (BeansException e) {
            logger.error("查询配件分类失败", e);
            ResponseDTO.fail("查询配件分类失败");
        }

        return ResponseDTO.success(partCategoryDRO, "OK");
    }

    /**
     * 描述:通过一级配件分类ID获取二级配件分类简单列表
     *
     * @param parentCategoryId
     *            一级分类的Id
     * @return ResponseDTO<PartCategoryDRO>
     * @author DaiZhichao
     * @since 2018/10/25 9:55
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listSecondLevelSimpleCategoryByParentCategoryId(Integer parentCategoryId) {
        if (Objects.isNull(parentCategoryId)) {
            logger.error(String.format("[%s]参数:[%s]不能为空", TAG, "parentCategoryId"));
            return ResponseDTO.fail("一级分类ID不能为空");
        }
        return ResponseDTO.success(Optional.ofNullable(partCategoryService.getPartCategorySimpleList(parentCategoryId)).orElse(new ArrayList<>(0)), "OK");
    }

    /**
     * 描述:通过一级配件分类ID获取二级配件分类详情列表
     *
     * @param parentCategoryId
     *            一级分类的Id
     * @return ResponseDTO<PartCategoryDRO>
     * @author DaiZhichao
     * @since 2018/10/25 9:55
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<PartCategoryDRO>> listSecondLevelCategoryByParentCategoryId(Integer parentCategoryId) {
        List<PartCategory> categList = null;
        if (Objects.isNull(parentCategoryId)) {
            logger.error(String.format("[%s]参数:[%s]不能为空", TAG, "parentCategoryId"));
            return ResponseDTO.fail("一级分类ID不能为空");
        }
        try {
            categList = partCategoryBService.getCategList(parentCategoryId, GlobalDict.STATUS_ENABLE);
        } catch (Exception e) {
            logger.error("查询配件分类失败", e);
            return ResponseDTO.fail("查询配件分类失败");
        }
        return ResponseDTO.success(categoryToDTO(categList), "OK");
    }

    /**
     * 描述:分类列表转换为分类DTO列表
     *
     * @param categList
     *            List<PartCategory>
     * @return List<PartCategoryDRO>
     * @author DaiZhichao
     * @since 2018/10/31 11:13
     */

    private List<PartCategoryDRO> categoryToDTO(List<PartCategory> categList) {
        List<PartCategoryDRO> partCategoryDROList = new ArrayList<>(categList.size());
        BeanCopier copier = BeanCopier.create(PartCategory.class, PartCategoryDRO.class, false);
        categList.forEach(partCategory -> {
            PartCategoryDRO partCategoryDRO = new PartCategoryDRO();
            copier.copy(partCategory, partCategoryDRO, null);
            partCategoryDROList.add(partCategoryDRO);
        });

        return partCategoryDROList;
    }

}
